<?php


namespace WXAPI\Logic;
use Think\Model\RelationModel;
/**
 * 小程序订阅消息
 */
class WxsendLogic extends RelationModel{
    protected $appid;
    protected $secret;

    public function __construct()
    {
//        $plugin = M('wx_user')->find();
        $this->appid = 'wxa924b897c5f17a84';
        $this->secret = '5a8e239d3693ab3d85e4397fa9764988';

    }

    /**
     * 小程序订阅消息发送消息
     * @param string $openid
     * @param int $board_id 模板id 1=>支付成功2=》发货通知3=》订单取消通知4=》自提信息提交5=>确认收货6=》下单成功7=>修改价格
     * @return array $result
     */
    public function sendSubscribeMessage($openid,$board_id,$template_id,$SendData){
        $access_token=$this->getAccessToken();

        $data = [] ;
        if($board_id == 1){

            $data['data'] = [
                "character_string1"=>[
                    'value' => $SendData['order_sn']
                ],
                "name2"=>[
                    'value' => $SendData['nickname']
                ],
                'date4'=>[
                    'value'=>$SendData['time']
                ],
                'thing7'=>[
                    'value'=>$SendData['address']
                ],
                'amount9'=>[
                    'value'=>$SendData['price']
                ]
            ];
            $data['page'] = "pages/order_details/order_details?id=".$SendData['order_id'].'&user_id='.$SendData['user_id'];
        }elseif($board_id == 2){
            $data['data'] = [
                "character_string1"=>[
                    'value' => $SendData['order_sn']
                ],
                "name2"=>[
                    'value' => $SendData['nickname']
                ],
                "date4"=>[
                    'value' => $SendData['time']
                ],
                'thing6'=>[
                    'value'=>$SendData['store_name']
                ],
                'amount9'=>[
                    'value'=>$SendData['price']
                ],
            ];
            $data['page'] = "pages/order_details/order_details?id=".$SendData['order_id'].'&user_id='.$SendData['user_id'];
        }elseif($board_id == 9){
            $data['data'] = [
                "thing1"=>[
                    'value' => $SendData['live_title']
                ],
                "time2"=>[
                    'value' =>$SendData['start_time']
                ],
                "thing3"=>[
                    'value' =>"直播即将开始喽"
                ],
            ];
            $data['page'] = "/pages/bundle/live_page/live_page?room_id=".$SendData['live_id'];
        }

        $data['touser'] = $openid;
        $data['template_id'] = $template_id;
//        $data['miniprogram_state'] = 'developer';
//        var_dump($data);
        $result = $this->sendFunction($access_token,$data);
        return $result;
    }

    /**
     * 获取access_token
     * @return array $output
     */
    public function getAccessToken()
    {
        $wechat = M('wx_user')->find();
        $expire_time = $wechat['web_expires3'];
        if($expire_time > time() && $wechat['web_access_token3']){
            return $wechat['web_access_token3'];
        }


        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
        $headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);


        if(!empty($output['access_token'])){
            $data['web_access_token3'] = $output['access_token'];
            $data['web_expires3'] = time() + 1400;
            M('wx_user')->where(['id'=>$wechat['id']])->save($data);
        }

        return $output['access_token'];
    }

    /**
     * 发送
     * @param $access_token
     * @param $data
     * @return array $output
     */
    public function sendFunction($access_token,$data)
    {
        $data  = json_encode($data);
        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$access_token;
        $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output,true);
    }
}
